<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>margin使其水平居中</title>
    <style>
        /* margin让块级元素水平居中 */
        .header {
            width: 100px;
            height: 100px;
            background-color: red;
            margin: 50px auto;
        }

        /* margin让行内元素水平居中 */
        .header2 {
            display: block;
            background-color: blue;
            text-align: center;
            margin: 50px auto;
            width: 100px;
            height: 100px;

        }
    </style>

</head>

<body>
    <div class="header">文字</div>
    <div class="header2"><span>文字</span></div>
</body>

</html>